package com.example.activityservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.commonservice.Common.Result;
import com.example.activityservice.entity.Activity;
import com.example.activityservice.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("activity")
public class ActivityController {

    @Autowired
    private ActivityService activityService;

    /**
     * 新增活动（管理员）
     */
    @PostMapping("/add")
    public Result<String> addActivity(@RequestBody Activity activity) {
        activity.setStatus(1); // 默认状态：未发布
        activity.setCreateTime(LocalDateTime.now());
        activity.setUpdateTime(LocalDateTime.now());
        activityService.save(activity);
        return Result.success("活动新增成功，ID：" + activity.getId());
    }

    /**
     * 删除活动（管理员）
     */
    @PostMapping("/delete")
    public Result<String> deleteActivity(@RequestParam Long id) {
        boolean success = activityService.removeById(id);
        return success ? Result.success("活动删除成功") : Result.error("活动不存在，删除失败");
    }

    /**
     * 更新活动（管理员）
     */
    @PostMapping("/update")
    public Result<String> updateActivity(@RequestBody Activity activity) {
        activity.setUpdateTime(LocalDateTime.now());
        boolean success = activityService.updateById(activity);
        return success ? Result.success("活动更新成功") : Result.error("活动不存在，更新失败");
    }

    /**
     * 发布活动（管理员）
     */
    @PostMapping("/publish/{id}")
    public Result<String> publishActivity(@PathVariable Long id) {
        boolean success = activityService.publishActivity(id);
        return success ? Result.success("活动发布成功") : Result.error("活动不存在，发布失败");
    }

    /**
     * 条件分页查询活动（通用接口）
     */
    @GetMapping("/list")
    public Result<IPage<Activity>> listActivities(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String title,
            @RequestParam(required = false) Integer status) {

        Page<Activity> page = new Page<>(current, size);
        QueryWrapper<Activity> wrapper = new QueryWrapper<>();
        wrapper.like(title != null, "title", title)
                .eq(status != null, "status", status);

        return Result.success(activityService.page(page, wrapper));
    }

    /**
     * 查询已发布的活动列表（志愿者/活动负责人）
     */
    @GetMapping("/list/published")
    public Result<List<Activity>> listPublishedActivities() {
        QueryWrapper<Activity> wrapper = new QueryWrapper<>();
        wrapper.eq("status", 2);
        return Result.success(activityService.list(wrapper));
    }

    @GetMapping("/getActivityName/{id}")
    public Result<String> getActivityName(@PathVariable Long id) {
        Activity activity = activityService.getById(id);
        return activity != null ? Result.success(activity.getTitle()) : Result.error("活动不存在");
    }
}
